package top.aoyudi.controller;

import top.aoyudi.service.IFileService;
import top.aoyudi.service.impl.FileServiceImpl;
import top.aoyudi.service.impl.UserServiceImpl;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class FileController {
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws URISyntaxException, IOException {
        System.out.println("*****欢迎来到小宇的文件系统****");
        System.out.println("\t*****请登录****");
        UserServiceImpl.login();
        // 获取主目录文件及文件夹
        IFileService fileService = new FileServiceImpl();
        fileService.userHomeDir();
        // 输出功能菜单
        FileServiceImpl.menu();
        System.out.print("******请选择菜单功能:");
        // 根据功能菜单选择对应功能
        int num = 0;
        try {
            num = scanner.nextInt();
        } catch (InputMismatchException e) {
            System.out.print("请输入数字\t\t");
        }
        // 用来接收\n
        String name = scanner.nextLine();
        while (num != 8) {
            switch (num) {
                case 1:// 进入文件夹
                    System.out.print("*****请输入进入的文件夹名称:");
                    name = scanner.nextLine();
                    System.out.printf(name);
                    fileService.openDir(name);
                    break;
                case 2:// 新建文件夹
                    System.out.print("*****请输入要新建的文件夹名称:");
                    name = scanner.nextLine();
                    fileService.addDir(name);
                    break;
                case 3:// 新建文件
                    System.out.print("*****请输入要新建的文件名(携带.txt后缀):");
                    name = scanner.nextLine();
                    fileService.addFile(name);
                    break;
                case 4:// 读文件
                    System.out.print("*****请输入要读取的文件的文件名:");
                    name = scanner.nextLine();
                    fileService.readFile(name);
                    break;
                case 5:// 写文件  覆盖文件还是在后面直接加分情况
                    System.out.print("*****请输入要写入文件的文件名:");
                    name = scanner.nextLine();
                    System.out.println("*****正在读取文件");
                    fileService.readFile(name);
                    System.out.print("*****请输入要写入文件的内容:");
                    String content = scanner.nextLine();
                    System.out.print("*****请选择内容已追加形式写入还是以覆盖形式写入(0:追加，1:覆盖):");
                    int type = scanner.nextInt();
                    // 用来接收\n
                    scanner.nextLine();
                    fileService.writeFile(name, content, type);
                    fileService.readFile(name);
                    break;
                case 6:// 删除文件/文件夹
                    System.out.print("*****请输入要删除的文件/文件夹名(文件带后缀):");
                    name = scanner.nextLine();
                    fileService.deleteFile(name);
                    break;
                case 7:// 返回上一层
                    fileService.backDir();
                    break;
                default:
                    System.out.println("\n\n****序号输入有误****\n\n");
            }
            // 输出当前目录文件
            fileService.userHomeDir();
            // 输出功能菜单
            FileServiceImpl.menu();
            System.out.print("*******请选择菜单功能:");
            // 根据功能菜单选择对应功能
            try {
                num = scanner.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("\n\n****请输入数字****\n\n");
            }
            // 用来接收\n
            scanner.nextLine();
        }
    }
}
